package com.purong.purong.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.purong.purong.R;
import com.purong.purong.bean.AreaBean;

import java.util.ArrayList;
import java.util.List;

public class ChoseAreaAdapter extends RecyclerView.Adapter<ChoseAreaAdapter.ViewHolder> {

    private List<AreaBean> itemList = new ArrayList<>();
    private  OnAreaListener mListener;
    private Context mContext;
    public ChoseAreaAdapter(Context context) {
        this.mContext = context;
    }
    public void setOnAreaListener(OnAreaListener listener){
        this.mListener=listener;
    }
    public void setData(List<AreaBean> itemList) {
//        if (Utils.isEmpty(imageList)) {
//            return;
//        }
        this.itemList = itemList;
    }

    /**
     * 通过ViewHolder可以访问每个item的所有控件
     */
    public static class ViewHolder extends RecyclerView.ViewHolder {
        //绑定每个CircleItem的控件引用到ViewHolder
        private TextView name,outline;
        private ImageView tag;
        private RelativeLayout mainLay;
        public ViewHolder(View v) {
            super(v);
            name=(TextView)v.findViewById(R.id.name);
            outline=(TextView)v.findViewById(R.id.outline);
            tag=(ImageView)v.findViewById(R.id.tag);
            mainLay=(RelativeLayout)v.findViewById(R.id.main_lay);
        }
    }


    // 创建每个Item的视图 (invoked by the item_circle manager)
    @Override
    public ChoseAreaAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                                                          int viewType) {
        //创建circleItem所对应的控件
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.chose_bank_item, parent, false);
        // set the view's size, margins, paddings and item_circle parameters
        ViewHolder vh = new ViewHolder(v);

        return vh;
    }

    // Replace the contents of a view (invoked by the item_circle manager)
    @Override
    public void onBindViewHolder(final ViewHolder mHolder, final int position) {
        mHolder.name.setText(itemList.get(position).getAcronym());

        if (itemList.get(position).isCheck()){
            mHolder.tag.setVisibility(View.VISIBLE);
            mHolder.name.setTextColor(mContext.getResources().getColor(R.color.blue));
        }else {
            mHolder.tag.setVisibility(View.GONE);
            mHolder.name.setTextColor(mContext.getResources().getColor(R.color.text_dark_gray));
        };
        mHolder.mainLay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener!=null){
                    mListener.onClick(position);
                }
            }
        });
    }



    // 获取Circle数据的item个数
    @Override
    public int getItemCount() {
        return itemList.size();
    }

    public interface OnAreaListener{
        void onClick(int position);
    }
}


